package com.fingard.dsp.bank.directbank.cscb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 长沙银行-余额查询接口
 * @author alan
 * @date 2020-05-18
 * @version 1.0
 */
public class CSCB01Balance extends CSCB01Base implements IBalance {

    /**
     * 今日余额查询
     * @param p_req7506
     * @return
     * @throws Exception
     */
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            while (p_req7506.readRecord()) {
                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //拼接请求报文
                CSCB01Header bankHeader = new CSCB01Header();
                bankHeader.trCode = "200108";
                bankHeader.reqNo = p_req7506.reqHeader.transSeqID;
                bankHeader.fileFlag = "0";//文件标识 0-报文 1-文件
                writeCommonHeader(xmlWriter, bankHeader);
                XElement tmpXElement = new XElement("body", "");
                tmpXElement.addChild(new XElement("acno", p_req7506.readedDetail.actNum));
                tmpXElement.addChild(new XElement("cur_code", convertCurToBank(p_req7506.readedDetail.actCur)));
                xmlWriter.writeXElement(tmpXElement);
                xmlWriter.writeEndDocument();
                //发送请求并接收响应
                String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString(), p_req7506.reqHeader.transType, p_req7506.reqHeader.commandCode);
                //处理响应报文
                if (tmpRetBkStr[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/ap/head/ret_code");
                        String returnMsg = tmpBkReader.getFirstText("/ap/head/ret_info");
                        String succ_flag = tmpBkReader.getFirstText("/ap/head/succ_flag");
                        if ("0000".equals(returnCode) && "0".equals(succ_flag)) {
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = returnMsg;
                            tmpRetDetail.actCur = convertCurToATS(tmpBkReader.getFirstTextNotNull("/ap/body/cur_code"));//币种
                            tmpRetDetail.actNum = tmpBkReader.getFirstTextNotNull("/ap/body/acno");//银行账号
                            tmpRetDetail.actArea = p_req7506.readedDetail.actArea;//账号地区区域码
                            tmpRetDetail.currBal = tmpBkReader.getFirstTextNotNull("/ap/body/balance");//余额
                            tmpRetDetail.availBal = tmpBkReader.getFirstTextNotNull("/ap/body/use_balance");//可用余额
                            tmpRetDetail.frozenBal = tmpBkReader.getFirstTextNotNull("/ap/body/freeze_bal");//冻结余额
                        } else {
                            tmpRetDetail.respCode="-1";
                            tmpRetDetail.respInfo=returnMsg;
                        }
                    } else {
                        tmpRetDetail.respCode="-1";
                        tmpRetDetail.respInfo=tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode="-1";
                    tmpRetDetail.respInfo=tmpRetBkStr[1];
                }
                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
            }
        } catch (Exception e) {
            retAts = new RetAts7506BalQry(p_req7506.reqHeader);
            retAts.writeStart();
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            WriteBankLogLn("余额查询异常：" + e);
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
